package com.sz.service;

import com.codingapi.txlcn.tc.annotation.DTXPropagation;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.sz.mapper.BankCardMapper;
import com.sz.mapper.BankParticularsMapper;
import com.sz.mapper.OtherBankMapper;
import com.sz.model.BankCard;
import com.sz.model.BankParticulars;
import com.sz.model.OtherBank;
import com.sz.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BankCareServiceImpl implements IBankCareService {

    @Autowired
    private BankCardMapper bankCardMapper;

    @Autowired
    private BankParticularsMapper bankParticularsMapper;

    @Autowired
    private OtherBankMapper otherBankMapper;

    @Autowired
    private RegTranjFeign regTranjFeign;

    @Override
    public Long selectMoney(String ba) {
        return bankCardMapper.selectMoney(ba);
    }


    @Transactional(rollbackFor=Exception.class)//本地事务
    @Override
    //同行转账
    public int Transfer(String ba1, String ba2, Long tradingAmount) {
        System.out.println("tradingAmount = " + tradingAmount);
        //付款人银行卡
        BankCard bankCard = bankCardMapper.selectMoney2(ba1);
        //收款人银行卡
        BankCard bankCard2 = bankCardMapper.selectMoney2(ba2);
        int i=0;
        //双方账户存在
        if(bankCard!=null&&bankCard2!=null){
            //获取银行卡余额
            Long mon = bankCard.getBalance();
            int money=Math.toIntExact(mon);
            Long mon2 = bankCard2.getBalance();
            int money2=Math.toIntExact(mon2);
            //获取银行卡详情
            BankParticulars bankParticulars = bankParticularsMapper.selectTDealP(bankCard.getPkBankId());
            BankParticulars bankParticulars2 = bankParticularsMapper.selectTDealP(bankCard2.getPkBankId());
            Long balance=null;

            //获取每日额度
            Long accountQuota = bankParticularsMapper.getAccountQuota(ba1);

            //判断每日额度
            if(accountQuota>=tradingAmount){
                //同地
                if(bankParticulars.getBranchId()==bankParticulars2.getBranchId()){
                    //转账业务
                    //汇款用户
                    balance=money - tradingAmount-1;
                }else{
                    //异地
                    if(tradingAmount<400){
                        balance=money - tradingAmount-2;
                    }else if(tradingAmount<10000){
                        balance=money - tradingAmount-(tradingAmount/200);
                    }else{
                        balance=money - tradingAmount-50;
                    }
                }

                i = bankCardMapper.updateBankMoney(ba1, balance);
                System.out.println("balance = " + balance);
                //System.out.println(1/0);
                i+=bankCardMapper.updateBankMoney(ba2,money2+tradingAmount);
                System.out.println("收款人余额"+money2+tradingAmount);

            }else{
                System.out.println("额度不足，转账失败");
                i=0;
            }
        }else{
            return -1;
        }

        return i;
    }

    @Transactional//跨行转账
    @Override
    public int OtherBankTransfer(String ba1, String ba2, Long tradingAmount) {
        //付款人银行卡
        BankCard bankCard = bankCardMapper.selectMoney2(ba1);
        //其他银行
        OtherBank otherBank = otherBankMapper.selectOtherBank(ba2);

        //获取银行卡余额
        Long mon = bankCard.getBalance();
        int money=Math.toIntExact(mon);
        Long mon2 = otherBank.getBalance();
        int money2=Math.toIntExact(mon2);
        //获取银行卡详情
        BankParticulars bankParticulars = bankParticularsMapper.selectTDealP(bankCard.getPkBankId());

        //获取每日额度
        Long accountQuota = bankParticularsMapper.getAccountQuota(ba1);
        Long balance=null;
        int i=0;
        //判断额度
        if(accountQuota>=tradingAmount){
            if(tradingAmount<=100){
                balance=money - tradingAmount-1;
            }else if(tradingAmount<=5000){
                balance=money - tradingAmount-(tradingAmount/100);
            }else {
                balance=money - tradingAmount-50;
            }

            i = bankCardMapper.updateBankMoney(ba1, balance);
            System.out.println("balance = " + balance);
            //System.out.println(1/0);
            i+=otherBankMapper.updateBankMoney(ba2,money2+tradingAmount);
            System.out.println("收款人余额"+money2+tradingAmount);

        }else{
            System.out.println("额度不足，转账失败");
            i=0;
        }

        return i;
    }

    @Override
    public String getPhoneByAccount(String ba) {
        return bankCardMapper.getPhoneByAccount(ba);
    }

    @LcnTransaction(propagation = DTXPropagation.REQUIRED)
    @Transactional
    @Override
    public int RegTranBank(String acc1 ,Long amount) {
        BankCard bankCard = bankCardMapper.selectMoney2(acc1);
        int i = bankCardMapper.updateBankMoney(acc1, bankCard.getBalance() - amount);


        return i;
    }


}
